﻿using LiveCharts;
using LiveCharts.Wpf;
using log4net;
using Sunny.UI;
using System.Configuration;
using System.Diagnostics.CodeAnalysis;
using Excel = Microsoft.Office.Interop.Excel;

namespace JFBG

{
    public partial class Page6 : UIPage
    {
        private static readonly ILog log = LogManager.GetLogger(typeof(Page4));
        //X轴值数量初始值
        private long xCount = 500;
        //X轴值最大计数值
        private long xTimes = 60;

        private long yCount = 0;
        //通道初始值
        private int channelIndex = 0;
        //传感器类型
        private int sensorType = 0;
        //传感器位置
        private int sensorIndex = 0;
        //结果展示
        private int waveShowIndex = 0;

        private string waveFilePath = "D:\\聚芯光纤\\";

        private string waveFileName = "导出数据";

        private string waveFileNameDate = "";

        [DisallowNull]
        private Excel.Application excelApp;

        [DisallowNull]
        private Excel._Workbook workbook;

        private List<NormalSensorEntity> normalList;

        private List<ResultSensorEntity> resultList;


        public Page6()
        {
            InitializeComponent();
            InitChartConfig(0);

            #region  多Y轴曲线
            //将自定义类型MeasureModel映射到曲线坐标点
            var mapper = LiveCharts.Configurations.Mappers.Xy<DataPoint>()
              .X(model => model.Times)    //使用 Time.作为X轴的标签
              .Y(model => model.Value);  //使用Value 作为Y轴的值

            //全局保存映射器
            Charting.For<DataPoint>(mapper);
            #endregion

            this.cbWaveChannelSellect.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveChannelSellect"]);
            this.cbWaveSensorSellect.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveSensorSellect"]);
            this.cbWaveSensorType.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveSensorType"]);
            this.cbWaveShowSellect.SelectedIndex = Convert.ToInt32(ConfigurationManager.AppSettings["cbWaveShowSellect"]);

        }
        private void InitChartConfig(int channelID)
        {
            try
            {
                string title = "通道-" + (channelID + 1);
                // 假设你在非UI线程上
                if (OscChart.InvokeRequired)
                {
                    OscChart.Invoke(new System.Action(() =>
                    {
                        OscChart.Series.Add(new StackedAreaSeries
                        {
                            Title = title,
                            PointGeometry = DefaultGeometries.Circle,
                            PointGeometrySize = 15,
                            StrokeThickness = 4,
                            LineSmoothness = 1,
                            PointForeground = System.Windows.Media.Brushes.Blue,
                            Foreground = System.Windows.Media.Brushes.Black,
                            FontSize = 20,
                            Values = InitChartValuesEmptyData(),
                            ScalesYAt = channelID
                        });
                        OscChart.AxisY.Add(new LiveCharts.Wpf.Axis
                        {
                            Title = "dBm",
                            FontSize = 12,
                            Foreground = System.Windows.Media.Brushes.DodgerBlue,
                            Position = AxisPosition.LeftBottom
                        });
                        OscChart.AxisX.Add(new LiveCharts.Wpf.Axis
                        {
                            //Title = "时间",
                            FontSize = 12,
                            DisableAnimations = true,
                            Foreground = System.Windows.Media.Brushes.Black,
                            LabelFormatter = value => value.ToString(),
                            //LabelFormatter = value => new System.DateTime(System.DateTime.Now.Ticks - (long)(this.xNumber - (long)value) * this.intervalTime * 10000).ToString("mm:ss"),
                            Separator = new Separator
                            {
                                Step = 1,
                                IsEnabled = true
                            },
                            MaxValue = this.xCount,
                            MinValue = 0
                        });
                    }));
                }
                else
                {
                    OscChart.Series.Add(new StackedAreaSeries
                    {
                        Title = title,
                        PointGeometry = DefaultGeometries.Circle,
                        PointGeometrySize = 15,
                        StrokeThickness = 4,
                        LineSmoothness = 1,
                        PointForeground = System.Windows.Media.Brushes.Blue,
                        Foreground = System.Windows.Media.Brushes.Black,
                        FontSize = 20,
                        Values = InitChartValuesEmptyData(),
                        ScalesYAt = channelID
                    });
                    OscChart.AxisY.Add(new LiveCharts.Wpf.Axis
                    {
                        Title = "dBm",
                        FontSize = 12,
                        Foreground = System.Windows.Media.Brushes.DodgerBlue,
                        Position = AxisPosition.LeftBottom
                    });
                    OscChart.AxisX.Add(new LiveCharts.Wpf.Axis
                    {
                        //Title = "时间",
                        FontSize = 12,
                        DisableAnimations = true,
                        Foreground = System.Windows.Media.Brushes.Black,
                        LabelFormatter = value => value.ToString(),
                        //LabelFormatter = value => new System.DateTime(System.DateTime.Now.Ticks - (long)(this.xNumber - (long)value) * this.intervalTime * 10000).ToString("mm:ss"),
                        Separator = new Separator
                        {
                            Step = 1,
                            IsEnabled = true
                        },
                        MaxValue = this.xCount,
                        MinValue = 0
                    });
                }
            }
            catch(Exception ex)
            { 
                log.Error(ex.Message);
            }
        }

        private ChartValues<DataPoint> InitChartValuesData()
        {
            ChartValues<DataPoint> chartValues = new ChartValues<DataPoint>();
            for (long i = 1; i <= this.xCount; i++)
            {
                chartValues.Add(new DataPoint(i, 1550.00001D));
            }
            this.xTimes = this.xCount;
            return chartValues;
        }

        private ChartValues<DataPoint> InitChartValuesEmptyData()
        {
            ChartValues<DataPoint> chartValues = new ChartValues<DataPoint>();
            this.xTimes = 0;
            return chartValues;
        }

        private void ChartLineRefresh(int index, double walue)
        {
            try
            {
                // 假设你在非UI线程上
                if (OscChart.InvokeRequired)
                {
                    OscChart.Invoke(new System.Action(() =>
                    {
                        //添加y值，pointValue1为波长1数据
                        var pointValue = new DataPoint(this.xTimes, walue);
                        OscChart.Series[0].Values.Add(pointValue);
                        //int _count = OscChart.Series[0].Values.Count;
                        //if (_count > this.xCount)
                        //{
                        //    //只关心前面30个数据
                        //    OscChart.Series[0].Values.RemoveAt(0);
                        //}
                        //在这里进行UI相关的操作，例如修改OscChart的属性等
                        OscChart.AxisX[0].MaxValue = this.xTimes;
                        OscChart.AxisX[0].MinValue = 0;// this.xTimes - this.xCount;
                        OscChart.Refresh();
                        this.xTimes++;
                    }));
                }
                else
                {
                    //添加y值，pointValue1为波长1数据
                    var pointValue = new DataPoint(this.xTimes, walue);
                    OscChart.Series[0].Values.Add(pointValue);
                    //int _count = OscChart.Series[0].Values.Count;
                    //if (_count > this.xCount)
                    //{
                    //    //只关心前面30个数据
                    //    OscChart.Series[0].Values.RemoveAt(0);
                    //}
                    // 直接进行操作，因为当前线程就是UI线程
                    OscChart.AxisX[0].MaxValue = this.xTimes;
                    OscChart.AxisX[0].MinValue = 0;// this.xTimes - this.xCount;
                    OscChart.Refresh();
                    this.xTimes++;
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        private void btnQuery_Click(object sender, EventArgs e)
        {
            //bool result = Sense2020.DLL_Open_Device();
            //log.Info(result);
            channelIndex = cbWaveChannelSellect.SelectedIndex + 1;
            sensorType = cbWaveSensorType.SelectedIndex;
            sensorIndex = cbWaveSensorSellect.SelectedIndex + 1;
            waveShowIndex = cbWaveShowSellect.SelectedIndex;
            DateTime startTime = startDatetime.Value;
            DateTime endTime = endDatetime.Value;
            QuerySqlData(sensorType, channelIndex, sensorIndex, startTime, endTime);
        }
        public void QuerySqlData(int sensorType, int channelIndex, int sensorIndex, DateTime startTime, DateTime endTime)
        {
            try
            {
                if (sensorType == 0)
                {
                    var sql = @"select * from raster_wave_normal where 1=1 ";
                    if (channelIndex > 0)
                    {
                        sql = sql + @" and channel_id=" + channelIndex;
                    }
                    sql = sql + @" and collect_time > " + startTime.DateTimeString() + @" and collect_time < " + endTime.DateTimeString() + @" order by collect_time limit " + this.xCount + @";";;
                    var dt = MainForm.db.SelectDataTable(sql);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        var row = dt.Rows[i];
                        var func = new NormalSensorEntity()
                        {
                            id = (long)row["id"],
                            channel_id = (int)row["channel_id"],
                            raster_type = (int)row["raster_type"],
                            raster_count = (int)row["raster_count"],
                            collect_time = (long)row["collect_time"],
                            wave_length1 = row["wave_length1"] != DBNull.Value ? Convert.ToDouble(row["wave_length1"]) : 0.0,//(double)row["wave_length1"],
                            wave_length2 = row["wave_length2"] != DBNull.Value ? Convert.ToDouble(row["wave_length2"]) : 0.0,//(double)row["wave_length2"],
                            wave_length3 = row["wave_length3"] != DBNull.Value ? Convert.ToDouble(row["wave_length3"]) : 0.0,//(double)row["wave_length3"],
                            wave_length4 = row["wave_length4"] != DBNull.Value ? Convert.ToDouble(row["wave_length4"]) : 0.0,//(double)row["wave_length4"],
                            wave_length5 = row["wave_length5"] != DBNull.Value ? Convert.ToDouble(row["wave_length5"]) : 0.0,//(double)row["wave_length5"],
                            wave_length6 = row["wave_length6"] != DBNull.Value ? Convert.ToDouble(row["wave_length6"]) : 0.0,//(double)row["wave_length6"],
                            wave_length7 = row["wave_length7"] != DBNull.Value ? Convert.ToDouble(row["wave_length7"]) : 0.0,//(double)row["wave_length7"],
                            wave_length8 = row["wave_length8"] != DBNull.Value ? Convert.ToDouble(row["wave_length8"]) : 0.0,//(double)row["wave_length8"],
                            wave_length9 = row["wave_length9"] != DBNull.Value ? Convert.ToDouble(row["wave_length9"]) : 0.0,//(double)row["wave_length9"],
                            wave_length10 = row["wave_length10"] != DBNull.Value ? Convert.ToDouble(row["wave_length10"]) : 0.0,//(double)row["wave_length10"],
                            wave_length11 = row["wave_length11"] != DBNull.Value ? Convert.ToDouble(row["wave_length11"]) : 0.0,//(double)row["wave_length11"],
                            wave_length12 = row["wave_length12"] != DBNull.Value ? Convert.ToDouble(row["wave_length12"]) : 0.0,//(double)row["wave_length12"],
                            wave_length13 = row["wave_length13"] != DBNull.Value ? Convert.ToDouble(row["wave_length13"]) : 0.0,//(double)row["wave_length13"],
                            wave_length14 = row["wave_length14"] != DBNull.Value ? Convert.ToDouble(row["wave_length14"]) : 0.0,//(double)row["wave_length14"],
                            wave_length15 = row["wave_length15"] != DBNull.Value ? Convert.ToDouble(row["wave_length15"]) : 0.0,//(double)row["wave_length15"],
                            wave_length16 = row["wave_length16"] != DBNull.Value ? Convert.ToDouble(row["wave_length16"]) : 0.0,//(double)row["wave_length16"]
                        };
                        normalList.Add(func);
                    }
                    ShowSensorChartData(normalList, sensorIndex);
                }
                else
                {
                    var sql = @"";
                    resultList = new List<ResultSensorEntity>();
                    if (sensorType == 1)//应变传感器
                    {
                        sql = @"select * from raster_wave_strain where 1=1 ";
                    }
                    else if (sensorType == 2)//倾角传感器
                    {
                        sql = @"select * from raster_wave_angle where 1=1 ";
                    }
                    else if (sensorType == 3)//加速度传感器
                    {
                        sql = @"select * from raster_wave_accele where 1=1 ";
                    }
                    else if (sensorType == 4)//温度传感器
                    {
                        sql = @"select * from raster_wave_temperate where 1=1 ";
                    }
                    if (channelIndex > 0)
                    {
                        sql = sql + @" and channel_id=" + channelIndex;
                    }
                    sql = sql + @" and collect_time > " + ToUnixTimestampMilliseconds(startTime) + @" and collect_time < " + ToUnixTimestampMilliseconds(endTime) + @" order by collect_time limit " + this.xCount + @";";
                    var dt = MainForm.db.SelectDataTable(sql);

                    for (int i = 0; i < dt.Rows.Count; i++)
                    {
                        var row = dt.Rows[i];
                        var func = new ResultSensorEntity()
                        {
                            id = (long)row["id"],
                            channel_id = (int)row["channel_id"],
                            raster_type = (int)row["raster_type"],
                            raster_count = (int)row["raster_count"],
                            collect_time = (long)row["collect_time"],
                            wave_length1 = row["wave_length1"] != DBNull.Value ? Convert.ToDouble(row["wave_length1"]) : 0.0,//(double)row["wave_length1"],
                            wave_length2 = row["wave_length2"] != DBNull.Value ? Convert.ToDouble(row["wave_length2"]) : 0.0,//(double)row["wave_length2"],
                            wave_length3 = row["wave_length3"] != DBNull.Value ? Convert.ToDouble(row["wave_length3"]) : 0.0,//(double)row["wave_length3"],
                            wave_length4 = row["wave_length4"] != DBNull.Value ? Convert.ToDouble(row["wave_length4"]) : 0.0,//(double)row["wave_length4"],
                            wave_length5 = row["wave_length5"] != DBNull.Value ? Convert.ToDouble(row["wave_length5"]) : 0.0,//(double)row["wave_length5"],
                            wave_length6 = row["wave_length6"] != DBNull.Value ? Convert.ToDouble(row["wave_length6"]) : 0.0,//(double)row["wave_length6"],
                            wave_length7 = row["wave_length7"] != DBNull.Value ? Convert.ToDouble(row["wave_length7"]) : 0.0,//(double)row["wave_length7"],
                            wave_length8 = row["wave_length8"] != DBNull.Value ? Convert.ToDouble(row["wave_length8"]) : 0.0,//(double)row["wave_length8"],
                            wave_length9 = row["wave_length9"] != DBNull.Value ? Convert.ToDouble(row["wave_length9"]) : 0.0,//(double)row["wave_length9"],
                            wave_length10 = row["wave_length10"] != DBNull.Value ? Convert.ToDouble(row["wave_length10"]) : 0.0,//(double)row["wave_length10"],
                            wave_length11 = row["wave_length11"] != DBNull.Value ? Convert.ToDouble(row["wave_length11"]) : 0.0,//(double)row["wave_length11"],
                            wave_length12 = row["wave_length12"] != DBNull.Value ? Convert.ToDouble(row["wave_length12"]) : 0.0,//(double)row["wave_length12"],
                            wave_length13 = row["wave_length13"] != DBNull.Value ? Convert.ToDouble(row["wave_length13"]) : 0.0,//(double)row["wave_length13"],
                            wave_length14 = row["wave_length14"] != DBNull.Value ? Convert.ToDouble(row["wave_length14"]) : 0.0,//(double)row["wave_length14"],
                            wave_length15 = row["wave_length15"] != DBNull.Value ? Convert.ToDouble(row["wave_length15"]) : 0.0,//(double)row["wave_length15"],
                            wave_length16 = row["wave_length16"] != DBNull.Value ? Convert.ToDouble(row["wave_length16"]) : 0.0,//(double)row["wave_length16"]
                            count_value1 = row["count_value1"] != DBNull.Value ? Convert.ToDouble(row["count_value1"]) : 0.0,//(double)row["count_value1"],
                            count_value2 = row["count_value2"] != DBNull.Value ? Convert.ToDouble(row["count_value2"]) : 0.0,//(double)row["count_value2"],
                            count_value3 = row["count_value3"] != DBNull.Value ? Convert.ToDouble(row["count_value3"]) : 0.0,//(double)row["count_value3"],
                            count_value4 = row["count_value4"] != DBNull.Value ? Convert.ToDouble(row["count_value4"]) : 0.0,//(double)row["count_value4"],
                            count_value5 = row["count_value5"] != DBNull.Value ? Convert.ToDouble(row["count_value5"]) : 0.0,//(double)row["count_value5"],
                            count_value6 = row["count_value6"] != DBNull.Value ? Convert.ToDouble(row["count_value6"]) : 0.0,//(double)row["count_value6"],
                            count_value7 = row["count_value7"] != DBNull.Value ? Convert.ToDouble(row["count_value7"]) : 0.0,//(double)row["count_value7"],
                            count_value8 = row["count_value8"] != DBNull.Value ? Convert.ToDouble(row["count_value8"]) : 0.0,//(double)row["count_value8"],
                            count_value9 = row["count_value9"] != DBNull.Value ? Convert.ToDouble(row["count_value9"]) : 0.0,//(double)row["count_value9"],
                            count_value10 = row["count_value10"] != DBNull.Value ? Convert.ToDouble(row["count_value10"]) : 0.0,//(double)row["count_value10"],
                            count_value11 = row["count_value11"] != DBNull.Value ? Convert.ToDouble(row["count_value11"]) : 0.0,//(double)row["count_value11"],
                            count_value12 = row["count_value12"] != DBNull.Value ? Convert.ToDouble(row["count_value12"]) : 0.0,//(double)row["count_value12"],
                            count_value13 = row["count_value13"] != DBNull.Value ? Convert.ToDouble(row["count_value13"]) : 0.0,//(double)row["count_value13"],
                            count_value14 = row["count_value14"] != DBNull.Value ? Convert.ToDouble(row["count_value14"]) : 0.0,//(double)row["count_value14"],
                            count_value15 = row["count_value15"] != DBNull.Value ? Convert.ToDouble(row["count_value15"]) : 0.0,//(double)row["count_value15"],
                            count_value16 = row["count_value16"] != DBNull.Value ? Convert.ToDouble(row["count_value16"]) : 0.0,//(double)row["count_value16"],
                        };
                        resultList.Add(func);
                    }
                    ShowSensorChartData(resultList, sensorIndex);
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        private void ShowSensorChartData(List<NormalSensorEntity> normalList, int sensorIndex)
        {
            try
            {            
                for (int i = 0; i < normalList.Count; i++)
                {
                    NormalSensorEntity sensorEntity = normalList[i];
                    if (sensorEntity != null)
                    {
                        if ((channelIndex > 0) && (channelIndex == sensorEntity.channel_id))
                        {
                            if (sensorIndex > 0)
                            {
                                switch (sensorIndex)
                                {
                                    case 1:
                                        if(!sensorEntity.wave_length1.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length1);
                                        }
                                        break;
                                    case 2:
                                        if (!sensorEntity.wave_length2.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length2);
                                        }
                                        break;
                                    case 3:
                                        if (!sensorEntity.wave_length3.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length3);
                                        }
                                        break;
                                    case 4:
                                        if (!sensorEntity.wave_length4.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length4);
                                        }
                                        break;
                                    case 5:
                                        if (!sensorEntity.wave_length5.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length5);
                                        }
                                        break;
                                    case 6:
                                        if (!sensorEntity.wave_length6.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length6);
                                        }
                                        break;
                                    case 7:
                                        if (!sensorEntity.wave_length7.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length7);
                                        }
                                        break;
                                    case 8:
                                        if (!sensorEntity.wave_length8.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length8);
                                        }
                                        break;
                                    case 9:
                                        if (!sensorEntity.wave_length9.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length9);
                                        }
                                        break;
                                    case 10:
                                        if (!sensorEntity.wave_length10.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length10);
                                        }
                                        break;
                                    case 11:
                                        if (!sensorEntity.wave_length11.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length11);
                                        }
                                        break;
                                    case 12:
                                        if (!sensorEntity.wave_length12.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length12);
                                        }
                                        break;
                                    case 13:
                                        if (!sensorEntity.wave_length13.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length13);
                                        }
                                        break;
                                    case 14:
                                        if (!sensorEntity.wave_length14.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length14);
                                        }
                                        break;
                                    case 15:
                                        if (!sensorEntity.wave_length15.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length15);
                                        }
                                        break;
                                    case 16:
                                        if (!sensorEntity.wave_length16.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length16);
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                            else
                            {
                                if (!sensorEntity.wave_length1.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length1);
                                }
                                if (!sensorEntity.wave_length2.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length2);
                                }
                                if (!sensorEntity.wave_length3.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length3);
                                }
                                if (!sensorEntity.wave_length4.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length4);
                                }
                                if (!sensorEntity.wave_length5.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length5);
                                }
                                if (!sensorEntity.wave_length6.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length6);
                                }
                                if (!sensorEntity.wave_length7.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length7);
                                }
                                if (!sensorEntity.wave_length8.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length8);
                                }
                                if (!sensorEntity.wave_length9.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length9);
                                }
                                if (!sensorEntity.wave_length10.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length10);
                                }
                                if (!sensorEntity.wave_length11.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length11);
                                }
                                if (!sensorEntity.wave_length12.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length12);
                                }
                                if (!sensorEntity.wave_length13.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length13);
                                }
                                if (!sensorEntity.wave_length14.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length14);
                                }
                                if (!sensorEntity.wave_length15.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length15);
                                }
                                if (!sensorEntity.wave_length15.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length16);
                                }
                            }
                        }
                        else
                        {
                            if (sensorIndex > 0)
                            {
                                switch (sensorIndex)
                                {
                                    case 1:
                                        if (!sensorEntity.wave_length1.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length1);
                                        }
                                        break;
                                    case 2:
                                        if (!sensorEntity.wave_length2.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length2);
                                        }
                                        break;
                                    case 3:
                                        if (!sensorEntity.wave_length3.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length3);
                                        }
                                        break;
                                    case 4:
                                        if (!sensorEntity.wave_length4.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length4);
                                        }
                                        break;
                                    case 5:
                                        if (!sensorEntity.wave_length5.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length5);
                                        }
                                        break;
                                    case 6:
                                        if (!sensorEntity.wave_length6.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length6);
                                        }
                                        break;
                                    case 7:
                                        if (!sensorEntity.wave_length7.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length7);
                                        }
                                        break;
                                    case 8:
                                        if (!sensorEntity.wave_length8.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length8);
                                        }
                                        break;
                                    case 9:
                                        if (!sensorEntity.wave_length9.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length9);
                                        }
                                        break;
                                    case 10:
                                        if (!sensorEntity.wave_length10.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length10);
                                        }
                                        break;
                                    case 11:
                                        if (!sensorEntity.wave_length11.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length11);
                                        }
                                        break;
                                    case 12:
                                        if (!sensorEntity.wave_length12.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length12);
                                        }
                                        break;
                                    case 13:
                                        if (!sensorEntity.wave_length13.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length13);
                                        }
                                        break;
                                    case 14:
                                        if (!sensorEntity.wave_length14.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length14);
                                        }
                                        break;
                                    case 15:
                                        if (!sensorEntity.wave_length15.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length15);
                                        }
                                        break;
                                    case 16:
                                        if (!sensorEntity.wave_length16.IsZero()){
                                            ChartLineRefresh(0, sensorEntity.wave_length16);
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                            else
                            {
                                if (!sensorEntity.wave_length1.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length1);
                                }
                                if (!sensorEntity.wave_length2.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length2);
                                }
                                if (!sensorEntity.wave_length3.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length3);
                                }
                                if (!sensorEntity.wave_length4.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length4);
                                }
                                if (!sensorEntity.wave_length5.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length5);
                                }
                                if (!sensorEntity.wave_length6.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length6);
                                }
                                if (!sensorEntity.wave_length7.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length7);
                                }
                                if (!sensorEntity.wave_length8.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length8);
                                }
                                if (!sensorEntity.wave_length9.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length9);
                                }
                                if (!sensorEntity.wave_length10.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length10);
                                }
                                if (!sensorEntity.wave_length11.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length11);
                                }
                                if (!sensorEntity.wave_length12.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length12);
                                }
                                if (!sensorEntity.wave_length13.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length13);
                                }
                                if (!sensorEntity.wave_length14.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length14);
                                }
                                if (!sensorEntity.wave_length15.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length15);
                                }
                                if (!sensorEntity.wave_length16.IsZero()){
                                    ChartLineRefresh(0, sensorEntity.wave_length16);
                                }
                            }
                        }
                    }
                }
            }
            catch(Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        private void ShowSensorChartData(List<ResultSensorEntity> resultList, int sensorIndex)
        {
            try
            {
                for (int index = 0; index < resultList.Count; index++)
                {
                    ResultSensorEntity sensorEntity = resultList[index];
                    if (sensorEntity != null)
                    {
                        if ((channelIndex > 0) && (channelIndex == sensorEntity.channel_id))
                        {
                            if (sensorIndex > 0)
                            {
                                if (waveShowIndex == 0)
                                {
                                    switch (sensorIndex)
                                    {
                                        case 1:
                                            if (!sensorEntity.wave_length1.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length1);
                                            }
                                            break;
                                        case 2:
                                            if (!sensorEntity.wave_length2.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length2);
                                            }
                                            break;
                                        case 3:
                                            if (!sensorEntity.wave_length3.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length3);
                                            }
                                            break;
                                        case 4:
                                            if (!sensorEntity.wave_length4.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length4);
                                            }
                                            break;
                                        case 5:
                                            if (!sensorEntity.wave_length5.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length5);
                                            }
                                            break;
                                        case 6:
                                            if (!sensorEntity.wave_length6.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length6);
                                            }
                                            break;
                                        case 7:
                                            if (!sensorEntity.wave_length7.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length7);
                                            }
                                            break;
                                        case 8:
                                            if (!sensorEntity.wave_length8.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length8);
                                            }
                                            break;
                                        case 9:
                                            if (!sensorEntity.wave_length9.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length9);
                                            }
                                            break;
                                        case 10:
                                            if (!sensorEntity.wave_length10.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length10);
                                            }
                                            break;
                                        case 11:
                                            if (!sensorEntity.wave_length11.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length11);
                                            }
                                            break;
                                        case 12:
                                            if (!sensorEntity.wave_length12.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length12);
                                            }
                                            break;
                                        case 13:
                                            if (!sensorEntity.wave_length13.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length13);
                                            }
                                            break;
                                        case 14:
                                            if (!sensorEntity.wave_length14.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length14);
                                            }
                                            break;
                                        case 15:
                                            if (!sensorEntity.wave_length15.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length15);
                                            }
                                            break;
                                        case 16:
                                            if (!sensorEntity.wave_length16.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length16);
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                else
                                {
                                    switch (sensorIndex)
                                    {
                                        case 1:
                                            if (!sensorEntity.count_value1.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value1);
                                            }
                                            break;
                                        case 2:
                                            if (!sensorEntity.count_value2.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value2);
                                            }
                                            break;
                                        case 3:
                                            if (!sensorEntity.count_value3.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value3);
                                            }
                                            break;
                                        case 4:
                                            if (!sensorEntity.count_value4.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value4);
                                            }
                                            break;
                                        case 5:
                                            if (!sensorEntity.count_value5.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value5);
                                            }
                                            break;
                                        case 6:
                                            if (!sensorEntity.count_value6.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value6);
                                            }
                                            break;
                                        case 7:
                                            if (!sensorEntity.count_value7.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value7);
                                            }
                                            break;
                                        case 8:
                                            if (!sensorEntity.count_value8.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value8);
                                            }
                                            break;
                                        case 9:
                                            if (!sensorEntity.count_value9.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value9);
                                            }
                                            break;
                                        case 10:
                                            if (!sensorEntity.count_value10.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value10);
                                            }
                                            break;
                                        case 11:
                                            if (!sensorEntity.count_value11.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value11);
                                            }
                                            break;
                                        case 12:
                                            if (!sensorEntity.count_value12.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value12);
                                            }
                                            break;
                                        case 13:
                                            if (!sensorEntity.count_value13.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value13);
                                            }
                                            break;
                                        case 14:
                                            if (!sensorEntity.count_value14.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value14);
                                            }
                                            break;
                                        case 15:
                                            if (!sensorEntity.count_value15.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value15);
                                            }
                                            break;
                                        case 16:
                                            if (!sensorEntity.count_value16.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value16);
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            else
                            {
                                if (waveShowIndex == 0)
                                {
                                    if (!sensorEntity.wave_length1.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length1);
                                    }
                                    if (!sensorEntity.wave_length2.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length2);
                                    }
                                    if (!sensorEntity.wave_length3.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length3);
                                    }
                                    if (!sensorEntity.wave_length4.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length4);
                                    }
                                    if (!sensorEntity.wave_length5.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length5);
                                    }
                                    if (!sensorEntity.wave_length6.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length6);
                                    }
                                    if (!sensorEntity.wave_length7.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length7);
                                    }
                                    if (!sensorEntity.wave_length8.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length8);
                                    }
                                    if (!sensorEntity.wave_length9.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length9);
                                    }
                                    if (!sensorEntity.wave_length10.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length10);
                                    }
                                    if (!sensorEntity.wave_length11.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length11);
                                    }
                                    if (!sensorEntity.wave_length12.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length12);
                                    }
                                    if (!sensorEntity.wave_length13.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length13);
                                    }
                                    if (!sensorEntity.wave_length14.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length14);
                                    }
                                    if (!sensorEntity.wave_length15.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length15);
                                    }
                                    if (!sensorEntity.wave_length16.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length16);
                                    }
                                }
                                else
                                {
                                    if (!sensorEntity.count_value1.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value1);
                                    }
                                    if (!sensorEntity.count_value2.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value2);
                                    }
                                    if (!sensorEntity.count_value3.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value3);
                                    }
                                    if (!sensorEntity.count_value4.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value4);
                                    }
                                    if (!sensorEntity.count_value5.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value5);
                                    }
                                    if (!sensorEntity.count_value6.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value6);
                                    }
                                    if (!sensorEntity.count_value7.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value7);
                                    }
                                    if (!sensorEntity.count_value8.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value8);
                                    }
                                    if (!sensorEntity.count_value9.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value9);
                                    }
                                    if (!sensorEntity.count_value10.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value10);
                                    }
                                    if (!sensorEntity.count_value11.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value11);
                                    }
                                    if (!sensorEntity.count_value12.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value12);
                                    }
                                    if (!sensorEntity.count_value13.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value13);
                                    }
                                    if (!sensorEntity.count_value14.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value14);
                                    }
                                    if (!sensorEntity.count_value15.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value15);
                                    }
                                    if (!sensorEntity.count_value16.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value16);
                                    }
                                }
                            }
                        }
                        else
                        {
                            if (sensorIndex > 0)
                            {
                                if (waveShowIndex == 0)
                                {
                                    switch (sensorIndex)
                                    {
                                        case 1:
                                            if (!sensorEntity.wave_length1.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length1);
                                            }
                                            break;
                                        case 2:
                                            if (!sensorEntity.wave_length2.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length2);
                                            }
                                            break;
                                        case 3:
                                            if (!sensorEntity.wave_length3.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length3);
                                            }
                                            break;
                                        case 4:
                                            if (!sensorEntity.wave_length4.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length4);
                                            }
                                            break;
                                        case 5:
                                            if (!sensorEntity.wave_length5.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length5);
                                            }
                                            break;
                                        case 6:
                                            if (!sensorEntity.wave_length6.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length6);
                                            }
                                            break;
                                        case 7:
                                            if (!sensorEntity.wave_length7.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length7);
                                            }
                                            break;
                                        case 8:
                                            if (!sensorEntity.wave_length8.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length8);
                                            }
                                            break;
                                        case 9:
                                            if (!sensorEntity.wave_length9.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length9);
                                            }
                                            break;
                                        case 10:
                                            if (!sensorEntity.wave_length10.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length10);
                                            }
                                            break;
                                        case 11:
                                            if (!sensorEntity.wave_length11.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length11);
                                            }
                                            break;
                                        case 12:
                                            if (!sensorEntity.wave_length12.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length12);
                                            }
                                            break;
                                        case 13:
                                            if (!sensorEntity.wave_length13.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length13);
                                            }
                                            break;
                                        case 14:
                                            if (!sensorEntity.wave_length14.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length14);
                                            }
                                            break;
                                        case 15:
                                            if (!sensorEntity.wave_length15.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length15);
                                            }
                                            break;
                                        case 16:
                                            if (!sensorEntity.wave_length16.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.wave_length16);
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                                else
                                {
                                    switch (sensorIndex)
                                    {
                                        case 1:
                                            if (!sensorEntity.count_value1.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value1);
                                            }
                                            break;
                                        case 2:
                                            if (!sensorEntity.count_value2.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value2);
                                            }
                                            break;
                                        case 3:
                                            if (!sensorEntity.count_value3.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value3);
                                            }
                                            break;
                                        case 4:
                                            if (!sensorEntity.count_value4.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value4);
                                            }
                                            break;
                                        case 5:
                                            if (!sensorEntity.count_value5.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value5);
                                            }
                                            break;
                                        case 6:
                                            if (!sensorEntity.count_value6.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value6);
                                            }
                                            break;
                                        case 7:
                                            if (!sensorEntity.count_value7.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value7);
                                            }
                                            break;
                                        case 8:
                                            if (!sensorEntity.count_value8.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value8);
                                            }
                                            break;
                                        case 9:
                                            if (!sensorEntity.count_value9.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value9);
                                            }
                                            break;
                                        case 10:
                                            if (!sensorEntity.count_value10.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value10);
                                            }
                                            break;
                                        case 11:
                                            if (!sensorEntity.count_value11.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value11);
                                            }
                                            break;
                                        case 12:
                                            if (!sensorEntity.count_value12.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value12);
                                            }
                                            break;
                                        case 13:
                                            if (!sensorEntity.count_value13.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value13);
                                            }
                                            break;
                                        case 14:
                                            if (!sensorEntity.count_value14.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value14);
                                            }
                                            break;
                                        case 15:
                                            if (!sensorEntity.count_value15.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value15);
                                            }
                                            break;
                                        case 16:
                                            if (!sensorEntity.count_value16.IsZero())
                                            {
                                                ChartLineRefresh(0, sensorEntity.count_value16);
                                            }
                                            break;
                                        default:
                                            break;
                                    }
                                }
                            }
                            else
                            {
                                if (waveShowIndex == 0)
                                {
                                    if (!sensorEntity.wave_length1.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length1);
                                    }
                                    if (!sensorEntity.wave_length2.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length2);
                                    }
                                    if (!sensorEntity.wave_length3.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length3);
                                    }
                                    if (!sensorEntity.wave_length4.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length4);
                                    }
                                    if (!sensorEntity.wave_length5.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length5);
                                    }
                                    if (!sensorEntity.wave_length6.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length6);
                                    }
                                    if (!sensorEntity.wave_length7.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length7);
                                    }
                                    if (!sensorEntity.wave_length8.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length8);
                                    }
                                    if (!sensorEntity.wave_length9.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length9);
                                    }
                                    if (!sensorEntity.wave_length10.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length10);
                                    }
                                    if (!sensorEntity.wave_length11.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length11);
                                    }
                                    if (!sensorEntity.wave_length12.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length12);
                                    }
                                    if (!sensorEntity.wave_length13.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length13);
                                    }
                                    if (!sensorEntity.wave_length14.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length14);
                                    }
                                    if (!sensorEntity.wave_length15.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length15);
                                    }
                                    if (!sensorEntity.wave_length16.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.wave_length16);
                                    }
                                }
                                else
                                {
                                    if (!sensorEntity.count_value1.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value1);
                                    }
                                    if (!sensorEntity.count_value2.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value2);
                                    }
                                    if (!sensorEntity.count_value3.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value3);
                                    }
                                    if (!sensorEntity.count_value4.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value4);
                                    }
                                    if (!sensorEntity.count_value5.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value5);
                                    }
                                    if (!sensorEntity.count_value6.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value6);
                                    }
                                    if (!sensorEntity.count_value7.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value7);
                                    }
                                    if (!sensorEntity.count_value8.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value8);
                                    }
                                    if (!sensorEntity.count_value9.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value9);
                                    }
                                    if (!sensorEntity.count_value10.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value10);
                                    }
                                    if (!sensorEntity.count_value11.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value11);
                                    }
                                    if (!sensorEntity.count_value12.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value12);
                                    }
                                    if (!sensorEntity.count_value13.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value13);
                                    }
                                    if (!sensorEntity.count_value14.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value14);
                                    }
                                    if (!sensorEntity.count_value15.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value15);
                                    }
                                    if (!sensorEntity.count_value16.IsZero())
                                    {
                                        ChartLineRefresh(0, sensorEntity.count_value16);
                                    }
                                }
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                log.Error(ex.Message);
            }
        }

        private void btnExport_Click(object sender, EventArgs e)
        {
            // 创建Excel应用程序实例
            this.excelApp = new Excel.Application();
            if (this.excelApp == null)
            {
                MessageBox.Show("无法创建excel对象，请确认excel环境是否正常");
                return;

            }
            this.excelApp.Visible = false;
            this.excelApp.UserControl = true;
            this.excelApp.DisplayAlerts = false;

            DateTime dt = DateTime.Today; // 获取今天的日期
            //DateTime yesterday = dt.AddDays(-1); // 获取前一天的日期
            string date = dt.Year.ToString("D4") + dt.Month.ToString("D2") + dt.Day.ToString("D2") + dt.Hour.ToString("D2") + dt.Minute.ToString("D2") + dt.Second.ToString("D2");
            Excel.Workbooks workbooks = this.excelApp.Workbooks;
            string filePath;
            if (this.waveFilePath == "")
            {
                filePath = AppDomain.CurrentDomain.BaseDirectory + this.waveFileName + @"-" + date + @".xlsx";
            }
            else
            {
                filePath = this.waveFilePath + this.waveFileName + @"-" + date + @".xlsx";
            }
            try
            {
                if (File.Exists(filePath))
                {
                    this.workbook = workbooks.Add(filePath); //加载模板
                }
                else
                {
                    File.Copy(AppDomain.CurrentDomain.BaseDirectory + this.waveFileName + @".xlsx", filePath, true);
                    this.workbook = workbooks.Add(filePath); //加载模板
                }
            }
            catch (Exception ex)
            {
                bool isUnlocked = FileUnlocker.UnlockFile(filePath);
                if (isUnlocked)
                {
                    this.workbook = workbooks.Add(filePath); //加载模板
                }
                else
                {
                    MessageBox.Show("存储 Excel 文件时出错,文件可能正被打开！\n" + ex.Message);
                    log.Error(ex.Message);
                    return;
                }
            }
            if (this.workbook == null)
            {
                MessageBox.Show("无法打开 Workbook 对象，请确认 Excel 文件是否正常");
                return;
            }

            if (!this.waveFileNameDate.Equals(date))
            {
                this.waveFileNameDate = date;
            }

            Excel.Sheets sheets = workbook.Sheets;
            Excel.Worksheet worksheet = (Excel.Worksheet)sheets.get_Item(1); //第一个工作薄。
            if (worksheet == null)
            {
                MessageBox.Show("无法打开 Worksheet 对象，请确认 Excel 文件是否正常");
                return; //工作薄中没有工作表.
            }
            worksheet.Name = date;

            int columnIndex = 1; // A列的索引是1
            int columnCount = 0;

            if ((resultList != null) && (resultList.Count > 0)){
                //写入数值
                for (int i = 0; i < resultList.Count; i++)
                {
                    ResultSensorEntity resultSensorEntity = resultList.Get(i);
                    //写入标题
                    worksheet.Cells[columnCount + 1, 1] = resultSensorEntity.collect_time.ToString() + ":" + resultSensorEntity.channel_id.ToString() + ":" + resultSensorEntity.raster_type.ToString() + ":" + resultSensorEntity.raster_count.ToString()
                         + ":" + resultSensorEntity.wave_length1.ToString() + ":" + resultSensorEntity.wave_length2.ToString() + ":" + resultSensorEntity.wave_length3.ToString() + ":" + resultSensorEntity.wave_length4.ToString()
                         + ":" + resultSensorEntity.wave_length5.ToString() + ":" + resultSensorEntity.wave_length6.ToString() + ":" + resultSensorEntity.wave_length7.ToString() + ":" + resultSensorEntity.wave_length8.ToString()
                         + ":" + resultSensorEntity.wave_length9.ToString() + ":" + resultSensorEntity.wave_length10.ToString() + ":" + resultSensorEntity.wave_length11.ToString() + ":" + resultSensorEntity.wave_length12.ToString()
                         + ":" + resultSensorEntity.wave_length13.ToString() + ":" + resultSensorEntity.wave_length14.ToString() + ":" + resultSensorEntity.wave_length15.ToString() + ":" + resultSensorEntity.wave_length16.ToString()
                         + ":" + resultSensorEntity.count_value1.ToString() + ":" + resultSensorEntity.count_value2.ToString() + ":" + resultSensorEntity.count_value3.ToString() + ":" + resultSensorEntity.count_value4.ToString()
                         + ":" + resultSensorEntity.count_value5.ToString() + ":" + resultSensorEntity.count_value6.ToString() + ":" + resultSensorEntity.count_value7.ToString() + ":" + resultSensorEntity.count_value8.ToString()
                         + ":" + resultSensorEntity.count_value9.ToString() + ":" + resultSensorEntity.count_value10.ToString() + ":" + resultSensorEntity.count_value11.ToString() + ":" + resultSensorEntity.count_value12.ToString()
                         + ":" + resultSensorEntity.count_value13.ToString() + ":" + resultSensorEntity.count_value14.ToString() + ":" + resultSensorEntity.count_value15.ToString() + ":" + resultSensorEntity.count_value16.ToString();
                    worksheet.Cells[columnCount + 1, i + 2] = resultSensorEntity.ToArrayString();
                }
            }

            if ((normalList != null) && (normalList.Count > 0))
            {
                //写入数值
                for (int i = 0; i < normalList.Count; i++)
                {
                    NormalSensorEntity normalSensorEntity = normalList.Get(i);
                    //写入标题
                    worksheet.Cells[columnCount + 1, 1] = "id:collect_time:channel_id:raster_count:wave_length1:wave_length2:wave_length3:wave_length4:wave_length5:wave_length6:wave_length7:wave_length8"
                         + ":wave_length9:wave_length10:wave_length11:wave_length12:wave_length13:wave_length14:wave_length15:wave_length16";
                    worksheet.Cells[columnCount + 1, i + 2] = normalSensorEntity.ToArrayString();
                }
            }

            worksheet.Columns.EntireColumn.AutoFit();//列宽自适应
            try
            {
                this.workbook.Saved = true;
                this.workbook.SaveAs(filePath);
            }
            catch (Exception ex)
            {
                bool isUnlocked = FileUnlocker.UnlockFile(filePath);
                if (isUnlocked)
                {
                    this.workbook.SaveAs(filePath);
                }
                else
                {
                    MessageBox.Show("存储 Excel 文件时出错,文件可能正被打开！\n" + ex.Message);
                    log.Error(ex.Message);
                    return;
                }
            }
        }

        private void cbWaveShowSellect_SelectedIndexChanged(object sender, EventArgs e)
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            AppSettingsSection app = config.AppSettings;
            app.Settings["cbWaveShowSellect"].Value = this.cbWaveShowSellect.SelectedIndex + "";
            config.Save(ConfigurationSaveMode.Modified);
        }

        public static long ToUnixTimestamp(DateTime dateTime)
        {
            return (long)Math.Floor((dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds);
        }

        public static long ToUnixTimestampMilliseconds(DateTime dateTime)
        {
            return (long)(dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds;
        }

        public class NormalSensorEntity
        {
            public long id { get; set; }

            public int channel_id { get; set; }

            public int raster_type { get; set; }

            public int raster_count { get; set; }

            public long collect_time { get; set; }

            public double wave_length1 { get; set; }

            public double wave_length2 { get; set; }

            public double wave_length3 { get; set; }

            public double wave_length4 { get; set; }

            public double wave_length5 { get; set; }

            public double wave_length6 { get; set; }

            public double wave_length7 { get; set; }

            public double wave_length8 { get; set; }

            public double wave_length9 { get; set; }

            public double wave_length10 { get; set; }

            public double wave_length11 { get; set; }

            public double wave_length12 { get; set; }

            public double wave_length13 { get; set; }

            public double wave_length14 { get; set; }

            public double wave_length15 { get; set; }

            public double wave_length16 { get; set; }


            public override string ToString()
            {
                return id + ", " + channel_id + ", " + raster_type + ", " + raster_count + ", " + collect_time
                    + ", " + wave_length1 + ", " + wave_length2 + ", " + wave_length3 + ", " + wave_length4
                    + ", " + wave_length5 + ", " + wave_length6 + ", " + wave_length7 + ", " + wave_length8
                    + ", " + wave_length9 + ", " + wave_length10 + ", " + wave_length11 + ", " + wave_length12
                    + ", " + wave_length13 + ", " + wave_length14 + ", " + wave_length15 + ", " + wave_length16;
            }

            public string[] ToArrayString()
            {
                string[] data = new String[]{
                                id.ToString(),channel_id.ToString(),raster_type.ToString(),raster_count.ToString(),collect_time.ToString(),
                                wave_length1.ToString(),wave_length2.ToString(),wave_length3.ToString(),wave_length4.ToString(),
                                wave_length5.ToString(),wave_length6.ToString(),wave_length7.ToString(),wave_length8.ToString(),
                                wave_length9.ToString(),wave_length10.ToString(),wave_length11.ToString(),wave_length12.ToString(),
                                wave_length13.ToString(),wave_length14.ToString(),wave_length15.ToString(),wave_length16.ToString()};
                return data;
            }
        }

        public class ResultSensorEntity : NormalSensorEntity
        {

            public double count_value1 { get; set; }

            public double count_value2 { get; set; }

            public double count_value3 { get; set; }

            public double count_value4 { get; set; }

            public double count_value5 { get; set; }

            public double count_value6 { get; set; }

            public double count_value7 { get; set; }

            public double count_value8 { get; set; }

            public double count_value9 { get; set; }

            public double count_value10 { get; set; }

            public double count_value11 { get; set; }

            public double count_value12 { get; set; }

            public double count_value13 { get; set; }

            public double count_value14 { get; set; }

            public double count_value15 { get; set; }

            public double count_value16 { get; set; }

            public override string ToString()
            {
                return id + ", " + channel_id + ", " + raster_type + ", " + raster_count + ", " + collect_time
                    + ", " + wave_length1 + ", " + wave_length2 + ", " + wave_length3 + ", " + wave_length4
                    + ", " + wave_length5 + ", " + wave_length6 + ", " + wave_length7 + ", " + wave_length8
                    + ", " + wave_length9 + ", " + wave_length10 + ", " + wave_length11 + ", " + wave_length12
                    + ", " + wave_length13 + ", " + wave_length14 + ", " + wave_length15 + ", " + wave_length16
                    + ", " + count_value1 + ", " + count_value2 + ", " + count_value3 + ", " + count_value4
                    + ", " + count_value5 + ", " + count_value6 + ", " + count_value7 + ", " + count_value8
                    + ", " + count_value9 + ", " + count_value10 + ", " + count_value11 + ", " + count_value12
                    + ", " + count_value13 + ", " + count_value14 + ", " + count_value15 + ", " + count_value16;
            }

            public string[] ToArrayString()
            {
                string[] data = new String[]{
                                id.ToString(),channel_id.ToString(),raster_type.ToString(),raster_count.ToString(),collect_time.ToString(),
                                wave_length1.ToString(),wave_length2.ToString(),wave_length3.ToString(),wave_length4.ToString(),
                                wave_length5.ToString(),wave_length6.ToString(),wave_length7.ToString(),wave_length8.ToString(),
                                wave_length9.ToString(),wave_length10.ToString(),wave_length11.ToString(),wave_length12.ToString(),
                                wave_length13.ToString(),wave_length14.ToString(),wave_length15.ToString(),wave_length16.ToString(),
                                count_value1.ToString(),count_value2.ToString(),count_value3.ToString(),count_value4.ToString(),
                                count_value5.ToString(),count_value6.ToString(),count_value7.ToString(),count_value8.ToString(),
                                count_value9.ToString(),count_value10.ToString(),count_value11.ToString(),count_value12.ToString(),
                                count_value13.ToString(),count_value14.ToString(),count_value15.ToString(),count_value16.ToString()};
                return data;
            }
        }
    }
}